<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/7/13
 * Time: 8:26
 */
namespace app\repository\wechat;

use app\repository\Repository;

class Wechat extends Repository
{
    private $_W;
    private $access_token;
    private $appid;
    private $appsecret;
    private $token;
    private $encodingaeskey;
    private $openid;

    public function __construct($openid = null)
    {
        $value = db('wx_config')->where([ 'key' => 'SHOPWCHAT'])->value('value');
        $value = json_decode($value);
        $this->token = $value->token;
        $this->appid = $value->appid;
        $this->appsecret = $value->appsecret;
        $this->openid = $openid;

        //获取并更新access_token
        $acc = db('wx_config')->where('key','ACCESS')->find();
        if($acc&&(time() - $acc['create_time'] <= 7100)){
            $this->access_token = $acc['value'];
        }else{
            $this->access();
        }
        $this->init();
    }

    private function init(){
        if($this->openid){
            $op = db('wx_fens_info')->where('openid',$this->openid)->find();
            if($op){
                $this->_W = $op;
            }else{
                //获取用户信息
                $this->userInfo();
            }
        }else{
            $this->openid = $this->openid();
            //获取用户信息
            $this->userInfo();
            $info = $this->_W;
            if($info['subscribe']==1){
                $row = db('wx_fens_info')->where('openid',$info['openid'])->find();
                if(!$row){
                    db('wx_fens_info')->insert([
                        'subscribe'=>$info['subscribe'],
                        'openid'=>$info['openid'],
                        'nickname'=>$info['nickname'],
                        'sex'=>$info['sex'],
                        'language'=>$info['language'],
                        'city'=>$info['city'],
                        'province'=>$info['province'],
                        'country'=>$info['country'],
                        'headimgurl'=>$info['headimgurl'],
                        'subscribe_scene'=>$info['subscribe_scene'],
                        'subscribe_time'=>$info['subscribe_time'],
                        'groupid'=>$info['groupid'],
                    ]);
                }
            }
        }
    }

    /**
     * 用户信息
     * @return mixed
     */
    public function W(){
        return $this->_W;
    }

    private function userInfo(){
        $re = httpRequest('https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $this->access_token . '&openid=' . $this->openid . '&lang=zh_CN', 'GET');
        $this->_W = (array)json_decode($re);
    }

    /**
     * 是否关注
     * @return bool
     */
    public function follow(){
        $info = $this->_W;
        if($info['subscribe']==1){
            return true;
        }
        return false;
    }

    /**
     * 获取openid
     * @return mixed
     */
    public function openid(){
        $op = new Openid($this->appid,$this->appsecret);
        return $op->GetOpenid();
    }

    /**
     * 获取access_token
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    private function access(){
        $t = httpRequest('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->appsecret,'GET');
        $t = json_decode($t);
        $this->access_token = $t->access_token;
        return db('wx_config')->where('key','ACCESS')->update(['create_time'=>time(),'value'=>$t->access_token]);
    }

}